package com.kay;

import com.kay.dao.UserMapper;
import com.kay.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Description: 测试 Mybatis整合Redis实现分布式二级缓存
 * @Author: Kay
 * @CreateDate: 2021/6/28$ 17:55$
 * @UpdateUser: Kay
 * @UpdateDate: 2021/6/28$ 17:55$
 */
public class RedisCacheTest {
    private SqlSessionFactoryBuilder sqlSessionFactoryBuilder;
    private InputStream resourceAsStream;

    @Before
    public void before() throws IOException {
        resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();

    }
    @Test
    public void testRedisLevelTwoSuccess(){
        SqlSessionFactory sessionFactory = sqlSessionFactoryBuilder.build(resourceAsStream);
        SqlSession sqlSession1 = sessionFactory.openSession();
        SqlSession sqlSession2 = sessionFactory.openSession();
        SqlSession sqlSession3 = sessionFactory.openSession(true);
        UserMapper userMapper1 = sqlSession1.getMapper(UserMapper.class);
        UserMapper userMapper2 = sqlSession2.getMapper(UserMapper.class);
        UserMapper userMapper3 = sqlSession3.getMapper(UserMapper.class);
        System.out.println("--------------执行SqlSession1创建的UserMapper1对象的查询---------------------");
        User user1 = userMapper1.findById(1);
        sqlSession1.close(); // 查询后关闭sqlSession1
        System.out.println("--------------执行SqlSession2创建的UserMapper2对象的查询---------------------");
        User user2 = userMapper2.findById(1);
        System.out.println("两次查询出来的对象 user1 user2 是否相等 ： " + (user1 == user2));
        sqlSession1.close();
    }
}
